<template>
  <div>
    <el-row
      type="flex"
      class="row-bg"
      justify="end">
      <el-radio-group v-model="dayweekmonth">
        <el-radio-button
          v-for="(item, index) in tabs"
          :key="index"
          :label="index">{{ tabs[index] }}</el-radio-button>
      </el-radio-group>
    </el-row>
    <div
      :class="className"
      :style="{height:height,width:width}" />
  </div>
</template>

<script>
import resize from '@/views/components/mixins/resize'
import echarts from 'echarts'
require('echarts/theme/infographic') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      default: function () {
        return {}
      },
      required: true
    },
    index: {
      type: Number,
      required: true
    },
    params: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      chart: null,
      dayweekmonth: 0,
      tabs: this.$i18n.t('appData.user.filterOptions')
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    },
    dayweekmonth: {
      deep: true,
      handler (val) {
        this.update()
      }
    },
    params: {
      deep: true,
      handler (val) {
        this.update()
      }
    }
  },
  mounted () {
    this.update()
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el.children[1], 'infographic')
      // this.setOptions(this.chartData)
    },
    update () {
      const { dayweekmonth, index } = this
      this.$emit('onTabChanged', { dayweekmonth, index })
    },
    formatChatData (data) {
      const xAxis = []
      const yAxis = []
      for (const y of data.value) {
        xAxis.push(y.time)
        yAxis.push(y.userCounts)
      }
      return { xAxis, yAxis }
    },
    setOptions (data = {}) {
      const { xAxis, yAxis } = this.formatChatData(data)
      if (!xAxis.length) {
        this.chart.showLoading(
          {
            text: this.$i18n.t('noData'),
            color: '#ffffff',
            textColor: '#8a8e91',
            maskColor: 'rgba(255, 255, 255, 0.8)'
          }
        )
      } else {
        this.chart.hideLoading()
      }
      const label = this.chartData.label
      this.chart.setOption({
        // title: {
        //   text: '用户走势图',
        //   textStyle: {
        //     fontWeight: 'normal'
        //   }
        // },
        xAxis: {
          data: xAxis.concat(),
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: [this.chartData.label]
        },
        series: [
          {
            name: label,
            itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: yAxis.concat(),
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }
        ]
      })
    }
  }
}
</script>
